package com.onehash.onebilling.gui;

import java.awt.*;

import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;



import com.onehash.onebilling.entity.*;
import java.io.IOException;
import java.io.FileNotFoundException;

public class OneBillingChPassDlg extends JDialog {
    JPanel JPchangePass = new JPanel();
    JLabel onebillingLogo = new JLabel();
    JLabel JLtitle = new JLabel();
    JLabel JLnote = new JLabel();
    JPanel JPusername = new JPanel();
    JLabel JLusername = new JLabel();
    JTextField JTusername = new JTextField();
    JPanel JPpassword = new JPanel();
    JLabel JLoldPassword = new JLabel();
    JLabel JLnewPassword = new JLabel();
    JLabel JLconfirmPassword = new JLabel();
    JPasswordField JPFoldPassword = new JPasswordField();
    JPasswordField JPFnewPassword = new JPasswordField();
    JPasswordField JPFconfirmPassword = new JPasswordField();
    JButton Bok = new JButton();
    JButton Bcancel = new JButton();
    JLabel imageLabel = new JLabel();

    /* Controller object */
    UserRegister userRegister;

    public OneBillingChPassDlg(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        try {
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            jbInit();
            pack();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public OneBillingChPassDlg() {
        this(new Frame(), "", false);
    }

    public void setControl(UserRegister userRegister){
        this.userRegister=userRegister;
    }

    private void jbInit() throws Exception {
        JPchangePass.setBackground(new Color(150, 150, 150));
        JPchangePass.setMaximumSize(new Dimension(627, 448));
        JPchangePass.setMinimumSize(new Dimension(627, 448));
        JPchangePass.setPreferredSize(new Dimension(627, 448));
        JPchangePass.setLayout(null);
        onebillingLogo.setIcon(new ImageIcon(OneBillingChPassDlg.class.getResource("images/logo.png")));
        onebillingLogo.setBounds(new Rectangle(3, 3, 149, 108));
        JLtitle.setFont(new java.awt.Font("Dialog", Font.BOLD, 22));
        JLtitle.setForeground(Color.orange);
        JLtitle.setText("Change Password");
        JLtitle.setBounds(new Rectangle(163, 54, 252, 25));
        JLnote.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLnote.setForeground(new Color(255, 80, 0));
        JLnote.setText(
                "* Notice: Do change your password periodically to prevent unauthorised " +
                "access.");
        JLnote.setBounds(new Rectangle(159, 84, 458, 14));
        JPusername.setBackground(Color.darkGray);
        JPusername.setBorder(BorderFactory.createRaisedBevelBorder());
        JPusername.setBounds(new Rectangle(115, 139, 413, 54));
        JPusername.setLayout(null);
        JLusername.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLusername.setForeground(Color.orange);
        JLusername.setText("Username:");
        JLusername.setBounds(new Rectangle(89, 19, 75, 14));
        JTusername.setBounds(new Rectangle(190, 15, 163, 23));
        JPpassword.setBackground(Color.gray);
        JPpassword.setBorder(BorderFactory.createRaisedBevelBorder());
        JPpassword.setBounds(new Rectangle(115, 192, 413, 171));
        JPpassword.setLayout(null);
        JLoldPassword.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLoldPassword.setForeground(Color.orange);
        JLoldPassword.setText("Old Password:");
        JLoldPassword.setBounds(new Rectangle(68, 23, 96, 14));
        JLnewPassword.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLnewPassword.setForeground(Color.orange);
        JLnewPassword.setText("New Password:");
        JLnewPassword.setBounds(new Rectangle(62, 67, 102, 14));
        JLconfirmPassword.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLconfirmPassword.setForeground(Color.orange);
        JLconfirmPassword.setText("Confirm Password:");
        JLconfirmPassword.setBounds(new Rectangle(42, 111, 122, 13));
        JPFoldPassword.setBounds(new Rectangle(188, 22, 163, 23));
        JPFnewPassword.setBounds(new Rectangle(188, 65, 163, 23));
        JPFconfirmPassword.setBounds(new Rectangle(188, 109, 163, 23));
        Bok.setBounds(new Rectangle(398, 391, 90, 32));
        Bok.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        Bok.setForeground(new Color(0, 53, 125));
        Bok.setMnemonic('O');
        Bok.setText("OK");
        Bok.addActionListener(new OneBillingChPassDlg_Bok_actionAdapter(this));
        Bcancel.setBounds(new Rectangle(507, 391, 90, 32));
        Bcancel.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        Bcancel.setForeground(new Color(0, 53, 125));
        Bcancel.setMnemonic('C');
        Bcancel.setText("Cancel");
        Bcancel.addActionListener(new OneBillingChPassDlg_Bcancel_actionAdapter(this));
        this.setResizable(false);
        imageLabel.setMinimumSize(new Dimension(627, 448));
        imageLabel.setPreferredSize(new Dimension(627, 448));
        imageLabel.setBounds(new Rectangle(0, 0, 627, 448));
        imageLabel.setIcon(new ImageIcon(OneBillingChPassDlg.class.getResource("images/BgChangePassword.jpg")));
        JPchangePass.add(onebillingLogo);
        JPchangePass.add(JLtitle);
        JPchangePass.add(JLnote);
        JPchangePass.add(JPusername);
        JPusername.add(JTusername);
        JPusername.add(JLusername);
        JPchangePass.add(JPpassword);
        JPchangePass.add(Bok);
        JPchangePass.add(Bcancel);
        JPchangePass.add(imageLabel);
        JPpassword.add(JPFoldPassword);
        JPpassword.add(JPFconfirmPassword);
        JPpassword.add(JPFnewPassword);
        JPpassword.add(JLconfirmPassword);
        JPpassword.add(JLoldPassword);
        JPpassword.add(JLnewPassword);
        this.getContentPane().add(JPchangePass);
        this.setSize(new Dimension(627, 448));
        this.center();
    }

    private void center(){
        //center the dialog
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public void Bcancel_actionPerformed(ActionEvent e) {
        this.dispose();
    }

    public void Bok_actionPerformed(ActionEvent e) {
        if(JTusername.getText().equals("")||
           String.valueOf(JPFoldPassword.getPassword()).equals("")||
           String.valueOf(JPFnewPassword.getPassword()).equals("")||
           String.valueOf(JPFconfirmPassword.getPassword()).equals("")){
            MessageDialog.showErrorDialog((Frame)this.getOwner(), this.getTitle(),
                    "Please fill in all fields.");
        }else{
            if (String.valueOf(JPFnewPassword.getPassword()).equals(String.
                    valueOf(JPFconfirmPassword.getPassword()))) {
                try {
                    if (userRegister.changePassword(JTusername.getText(),
                            String.valueOf(JPFoldPassword.getPassword()),
                            String.valueOf(JPFnewPassword.getPassword()))) {
                        MessageDialog.showInfoDialog((Frame)this.getOwner(),
                                this.getTitle(),
                                "Password has been changed successfully.");
                        this.dispose();
                    } else {
                        MessageDialog.showErrorDialog((Frame)this.getOwner(),
                                this.getTitle(),
                                "Incorrect login information provided. Password not changed.");
                    }
                } catch (FileNotFoundException fe) {
                    MessageDialog.showErrorDialog((Frame)this.getOwner(), this.getTitle(),
                            "Login Information File was not found. Please contact System Administrator.");
                    System.exit(1);
                } catch (IOException ie) {
                    MessageDialog.showErrorDialog((Frame)this.getOwner(), this.getTitle(),
                            "An I/O Error has occurred. Please contact System Administrator.");
                    System.exit(1);
                }
            }else{
                MessageDialog.showErrorDialog((Frame)this.getOwner(), this.getTitle(),
                        "New Password and Confirm Password must be the same.");
            }
        }
    }

}


class OneBillingChPassDlg_Bok_actionAdapter implements ActionListener {
    private OneBillingChPassDlg adaptee;
    OneBillingChPassDlg_Bok_actionAdapter(OneBillingChPassDlg adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Bok_actionPerformed(e);
    }
}


class OneBillingChPassDlg_Bcancel_actionAdapter implements ActionListener {
    private OneBillingChPassDlg adaptee;
    OneBillingChPassDlg_Bcancel_actionAdapter(OneBillingChPassDlg adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Bcancel_actionPerformed(e);
    }
}
